package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-08-19
 * Time: 17:37
 */
public class MaxLenghtStr {
    public static void main(String[] args) {
        System.out.println(longestCommonPrefix(new String[]{"flower", "flow", "flight"}));
    }

    public static String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length < 1) {
            return "";
        }
        if (strs.length == 1) {
            return strs[0];
        }

        StringBuilder resultStr = new StringBuilder();
        char[] charFirst = strs[0].toCharArray();
        String result = "";
        int strLenght = strs.length - 1;

        while (strLenght > 0) {
            char[] charCompare = strs[strLenght].toCharArray();

            int maxSize = charFirst.length > charCompare.length ? charCompare.length : charFirst.length;

            for (int i = 0; i < maxSize; i++) {
                if (charCompare[i] == charFirst[i]) {
                    resultStr.append(charCompare[i]);
                } else {
                    break;
                }
            }

            result = resultStr.toString();
            charFirst = resultStr.toString().toCharArray();
            resultStr = new StringBuilder("");
            strLenght--;
        }

        return result;
    }
}
